package com.summer.service.impl;

import com.google.gson.*;
import com.summer.entity.gongxinbao.tbdata.*;
import com.summer.entity.gongxinbao.tbreport.*;
import com.summer.entity.gongxinbao.zhima.RelData;
import com.summer.entity.gongxinbao.zhima.RelTbZhima;
import com.summer.mapper.gongxinbao.RelBaseInfoDao;
import com.summer.mapper.gongxinbao.RelTbZhimaMapper;
import com.summer.mapper.gongxinbao.tbdata.*;
import com.summer.mapper.gongxinbao.tbreport.*;
import com.summer.service.ITbRelativizeService;
import com.summer.variable.Constant;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tl on 2018/6/12.
 */
@Service
public class TbRelativizeService extends BaseRelativizeService implements ITbRelativizeService {
    private static Logger LOG = LoggerFactory.getLogger(TbRelativizeService.class);
    @Resource
    private RelTbBindedBankCardDao relTbBindedBankCardDao;
    @Resource
    private RelTbAddressDao relTbAddressDao;
    @Resource
    private RelTbHuabeiBillDao relTbHuabeiBillDao;
    @Resource
    private RelTbFeesAccountDao relTbFeesAccountDao;
    @Resource
    private RelTbHuabeiConsumeDao relTbHuabeiConsumeDao;
    @Resource
    private RelTbBankAssetDetailDao relTbBankAssetDetailDao;
    @Resource
    private RelTbBankLoanDetailDao relTbBankLoanDetailDao;
    @Resource
    private RelTbBankRepayPlanDao relTbBankRepayPlanDao;
    @Resource
    private RelTbUserDataDao relTbUserDataDao;
    @Resource
    private RelTbSoldSubOrderDao relTbSoldSubOrderDao;
    @Resource
    private RelTbTradeDao relTbTradeDao;
    @Resource
    private RelTbSubOrderDao relTbSubOrderDao;
    @Resource
    private RelTbTransferBankCardDao relTbTransferBankCardDao;
    @Resource
    private RelTbRptAddressDao relTbRptAddressDao;
    @Resource
    private RelTbRptBaseInfoDao relTbRptBaseInfoDao;
    @Resource
    private RelTbRptConsumptionDao relTbRptConsumptionDao;
    @Resource
    private RelTbRptIncomeDao relTbRptIncomeDao;
    @Resource
    private RelTbRptIntimacyPayDao relTbRptIntimacyPayDao;
    @Resource
    private RelTbRptRepaymentDao relTbRptRepaymentDao;
    @Resource
    private RelTbRptShopDao relTbRptShopDao;
    @Resource
    private RelBaseInfoDao relBaseInfoDao;
    @Resource
    private RelTbZhimaMapper relTbZhimaMapper;

    @Override
    public TbRelativizeResult getResult(String jsonBody) throws IllegalAccessException,
            NoSuchMethodException, InvocationTargetException {
        TbRelativizeResult tbRelativizeResult = new TbRelativizeResult();
        if (StringUtils.isBlank(jsonBody)) {
            LOG.info("TbRelativizeResult getResult null == jsonBody");
            return tbRelativizeResult;
        }
        JsonParser parser = new JsonParser();
        JsonObject jsonObject = parser.parse(jsonBody).getAsJsonObject();
//        String authJson = jsonObject.get("authJson").getAsJsonObject().toString();
        String authJson = jsonObject.get("authJson").getAsString();
        String name = jsonObject.get("name").getAsString();
        String idcard = jsonObject.get("idcard").getAsString();
        String phone = jsonObject.get("phone").getAsString();
        if (StringUtils.isBlank(authJson)) {
            LOG.info("TbRelativizeResult getResult null == authJson");
            return tbRelativizeResult;
        }
        UserEcommerceDataDTO jsonRootBean = null;
        JsonObject root = parser.parse(authJson).getAsJsonObject();
        if (root != null) {
            String result = root.toString();
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            try {
                jsonRootBean = gson.fromJson(result, UserEcommerceDataDTO.class);
            } catch (JsonSyntaxException ignored) {
                LOG.error("JsonSyntaxException tbRelativize identityCard=" + idcard);
            }
            if (null == jsonRootBean) {
                return tbRelativizeResult;
            }
            Long saveId = getBaseId(idcard, phone, name, Constant.RELATIVIZE_TYPE_GXBTB);
            if (null == saveId) {
                LOG.info("null == BaseId identityCard==" + idcard);
                return tbRelativizeResult;
            }
            EcommerceBaseInfoDTO ecommerceBaseInfo = jsonRootBean.getEcommerceBaseInfo();
            EcommerceJiebeiInfoDTO jiebeiInfo = jsonRootBean.getJiebeiInfo();
            EcommerceHuabeiInfoDTO huabeiInfo = jsonRootBean.getHuabeiInfo();
            MyBankBindInfoDTO myBankBindInfo = jsonRootBean.getMyBankBindInfo();
            RelTbUserData relTbUserData = new RelTbUserData();
            relTbUserData.setBaseId(saveId);
            if (null != ecommerceBaseInfo) {
                PropertyUtils.copyProperties(relTbUserData, ecommerceBaseInfo);
                relTbUserData.setHuabeiAmountBase(ecommerceBaseInfo.getHuabeiAmount());
                relTbUserData.setHuabeiBalanceBase(ecommerceBaseInfo.getHuabeiBalance());
                relTbUserData.setHuabeiStatusBase(ecommerceBaseInfo.getHuabeiStatus());
                relTbUserData.setHuabeiNextMonthPaymentBase(ecommerceBaseInfo.getHuabeiNextMonthPayment());

                relTbUserData.setHuabeiCurrentMonthPaymentBase(ecommerceBaseInfo.getHuabeiCurrentMonthPayment());
                relTbUserData.setHuabeiPenaltyAmountBase(ecommerceBaseInfo.getHuabeiPenaltyAmount());
                relTbUserData.setHuabeiOverdueDaysBase(ecommerceBaseInfo.getHuabeiOverdueDays());
                relTbUserData.setHuabeiPayDayBase(ecommerceBaseInfo.getHuabeiPayDay());

                relTbUserData.setHuabeiOverdueBillCntBase(ecommerceBaseInfo.getHuabeiOverdueBillCnt());
                relTbUserData.setHuabeiOriginalAmountBase(ecommerceBaseInfo.getHuabeiOriginalAmount());
                relTbUserData.setJiebeiAmountBase(ecommerceBaseInfo.getJiebeiAmount());
                relTbUserData.setJiebeiBalanceBase(ecommerceBaseInfo.getJiebeiBalance());
            }
            if (null != jiebeiInfo) {
                PropertyUtils.copyProperties(relTbUserData, jiebeiInfo);
            }
            if (null != huabeiInfo) {
                PropertyUtils.copyProperties(relTbUserData, huabeiInfo);
            }
            if (null != myBankBindInfo) {
                PropertyUtils.copyProperties(relTbUserData, myBankBindInfo);
            }
            relTbUserData.setMobileAll(phone);
            tbRelativizeResult.setRelTbUserData(relTbUserData);

            List<RelTbAddress> addressList = new ArrayList<>();
            getAddress(jsonRootBean, addressList, saveId);
            tbRelativizeResult.setRelTbAddressList(addressList);

            List<RelTbBindedBankCard> bindedBankCardList = new ArrayList<>();
            getBindedBankCard(jsonRootBean, bindedBankCardList, saveId);
            tbRelativizeResult.setRelTbBindedBankCardList(bindedBankCardList);

            List<RelTbFeesAccount> feesAccountList = new ArrayList<>();
            getFeesAccount(jsonRootBean, feesAccountList, saveId);
            tbRelativizeResult.setRelTbFeesAccountList(feesAccountList);

            List<RelTbHuabeiBill> huabeiBillList = new ArrayList<>();
            getHuabeiBill(jsonRootBean, huabeiBillList, saveId);
            tbRelativizeResult.setRelTbHuabeiBillList(huabeiBillList);

            List<RelTbHuabeiConsume> huabeiConsumeList = new ArrayList<>();
            getHuabeiConsume(jsonRootBean, huabeiConsumeList, saveId);
            tbRelativizeResult.setRelTbHuabeiConsumeList(huabeiConsumeList);

            List<RelTbSoldSubOrder> soldSubOrderList = new ArrayList<>();
            getSoldSubOrder(jsonRootBean, soldSubOrderList, saveId);
            tbRelativizeResult.setRelTbSoldSubOrderList(soldSubOrderList);

            List<RelTbSubOrder> subOrderList = new ArrayList<>();
            getSubOrder(jsonRootBean, subOrderList, saveId);
            tbRelativizeResult.setRelTbSubOrderList(subOrderList);

            List<RelTbTrade> tradeList = new ArrayList<>();
            getTrade(jsonRootBean, tradeList, saveId);
            tbRelativizeResult.setRelTbTradeList(tradeList);

            List<RelTbTransferBankCard> tbTransferBankCardList = new ArrayList<>();
            getTransferBankCard(jsonRootBean, tbTransferBankCardList, saveId);
            tbRelativizeResult.setRelTbTransferBankCardList(tbTransferBankCardList);

            List<RelTbBankAssetDetail> tbBankAssetDetailList = new ArrayList<>();
            getBankAssetDetail(jsonRootBean, tbBankAssetDetailList, saveId);
            tbRelativizeResult.setRelTbBankAssetDetailList(tbBankAssetDetailList);

            List<RelTbBankLoanDetail> tbBankLoanDetailList = new ArrayList<>();
            getBankLoanDetail(jsonRootBean, tbBankLoanDetailList, saveId);
            tbRelativizeResult.setRelTbBankLoanDetailList(tbBankLoanDetailList);

            List<RelTbBankRepayPlan> tbBankRepayPlanList = new ArrayList<>();
            getBankRepayPlan(jsonRootBean, tbBankRepayPlanList, saveId);
            tbRelativizeResult.setRelTbBankRepayPlanList(tbBankRepayPlanList);
        }
        return tbRelativizeResult;
    }

    @Override
    public TbReportRelativizeResult getReportResult(String jsonBody, String name) throws IllegalAccessException,
            NoSuchMethodException, InvocationTargetException {
        TbReportRelativizeResult tbReportRelativizeResult = new TbReportRelativizeResult();
        if (StringUtils.isBlank(jsonBody)) {
            LOG.info("TbReportRelativizeResult getReportResult null == jsonBody");
            return tbReportRelativizeResult;
        }
        BaseReportDTO baseReportDTO = null;
        String idcard = "";
        String phone = "";
        JsonParser parser = new JsonParser();
        JsonObject root = parser.parse(jsonBody).getAsJsonObject();
        if (root.has("data")) {
            JsonObject dataJson = root.get("data").getAsJsonObject();
            if (dataJson.has("authInfoVo")) {
                JsonObject authInfoVo = dataJson.get("authInfoVo").getAsJsonObject();
                if (authInfoVo != null) {
                    idcard = authInfoVo.get("idcard").getAsString();
                    phone = authInfoVo.get("phone").getAsString();
                }
            }
            if (dataJson.has("authResult")) {
                JsonObject authResult = dataJson.get("authResult").getAsJsonObject();
                if (authResult != null) {
                    String result = authResult.toString();
                    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                    try {
                        baseReportDTO = gson.fromJson(result, BaseReportDTO.class);
                    } catch (JsonSyntaxException ignored) {
                        LOG.error("JsonSyntaxException tbReportRelativize identityCard=" + idcard);
                    }
                }
            }
        }
        if (baseReportDTO == null || StringUtils.isBlank(idcard)) {
            LOG.info("TbReportRelativizeResult getReportResult null == baseReportDTO||null " +
                    "==idcard");
            return tbReportRelativizeResult;
        }
        Long saveId = getBaseId(idcard, phone, name, Constant.RELATIVIZE_TYPE_GXBTBRPT);
        if (null == saveId) {
            LOG.info("TbReportRelativizeResult null == BaseId identityCard==" + idcard);
            return tbReportRelativizeResult;
        }
        ReportSummary reportSummary = baseReportDTO.getReportSummary();
        ExpenditureReport expenditureReport = baseReportDTO.getExpenditureReport();
        IncomeTotalReport incomeReport = baseReportDTO.getIncomeReport();
        if (null != reportSummary) {
            RelTbRptBaseInfo relTbRptBaseInfo = new RelTbRptBaseInfo();
            relTbRptBaseInfo.setBaseId(saveId);
            BaseInfoReportDTO baseInfo = reportSummary.getBaseInfo();
            AssetsInfoDTO assetsInfo = reportSummary.getAssetsInfo();
            BehaviorInfoDTO behaviorInfo = reportSummary.getBehaviorInfo();
            if (null != baseInfo) {
                PropertyUtils.copyProperties(relTbRptBaseInfo, baseInfo);
                List<RelTbRptIntimacyPay> intimacyPayList = new ArrayList<>();
                getIntimacyPay(baseInfo, intimacyPayList, saveId);
                tbReportRelativizeResult.setRelTbRptIntimacyPayList(intimacyPayList);
            }
            if (null != assetsInfo) {
                PropertyUtils.copyProperties(relTbRptBaseInfo, assetsInfo);
            }
            if (null != behaviorInfo) {
                PropertyUtils.copyProperties(relTbRptBaseInfo, behaviorInfo);
            }
            relTbRptBaseInfo.setIdCard(idcard);
            relTbRptBaseInfo.setPhone(phone);
            tbReportRelativizeResult.setRelTbRptBaseInfo(relTbRptBaseInfo);

            List<RelTbRptAddress> addressList = new ArrayList<>();
            getRptAddress(reportSummary, addressList, saveId);
            tbReportRelativizeResult.setRelTbRptAddressList(addressList);

            List<RelTbRptShop> shopList = new ArrayList<>();
            getRptShop(reportSummary, shopList, saveId);
            tbReportRelativizeResult.setRelTbRptShopList(shopList);
        }
        if (null != expenditureReport) {
            List<RelTbRptConsumption> consumptionList = new ArrayList<>();
            getRptConsumption(expenditureReport, consumptionList, saveId);
            tbReportRelativizeResult.setRelTbRptConsumptionList(consumptionList);

            List<RelTbRptRepayment> repaymentList = new ArrayList<>();
            getRptRepayment(expenditureReport, repaymentList, saveId);
            tbReportRelativizeResult.setRelTbRptRepaymentList(repaymentList);
        }
        if (null != incomeReport) {
            List<RelTbRptIncome> incomeList = new ArrayList<>();
            getRptIncome(incomeReport, incomeList, saveId);
            tbReportRelativizeResult.setRelTbRptIncomeList(incomeList);
        }
        return tbReportRelativizeResult;
    }

    @Override
    public RelData getZhimaResult(String jsonBody, String name) throws IllegalAccessException,
            NoSuchMethodException, InvocationTargetException {
        RelData relData = null;
        if (StringUtils.isBlank(jsonBody)) {
            LOG.info("TbReportRelativizeResult getZhimaResult null == jsonBody");
            return null;
        }
        String idcard = "";
        String phone = "";
        JsonParser parser = new JsonParser();
        JsonObject root = parser.parse(jsonBody).getAsJsonObject();
        if (root.has("data")) {
            JsonObject dataJson = root.get("data").getAsJsonObject();
            if (dataJson.has("authInfoVo")) {
                JsonObject authInfoVo = dataJson.get("authInfoVo").getAsJsonObject();
                if (authInfoVo != null) {
                    idcard = authInfoVo.get("idcard").getAsString();
                    phone = authInfoVo.get("phone").getAsString();
                }
            }
            if (dataJson != null) {
                String result = dataJson.toString();
                Gson gson = new Gson();
                try {
                    relData = gson.fromJson(result, RelData.class);
                } catch (JsonSyntaxException ignored) {
                    LOG.error("JsonSyntaxException getZhimaResult identityCard=" + idcard);
                }
            }
        }
        if (relData == null || StringUtils.isBlank(idcard)) {
            LOG.info("TbReportRelativizeResult getZhimaResult null == baseReportDTO||null " +
                    "==idcard");
            return null;
        }
        Long saveId = getBaseId(idcard, phone, name, Constant.RELATIVIZE_TYPE_GXBZM);
        if (null == saveId) {
            LOG.info("getZhimaResult null == BaseId identityCard==" + idcard);
            return null;
        }
        for (RelTbZhima zmCredit : relData.getZmCredits()) {
            zmCredit.setPhone(phone);
        }
        return relData;
    }

    private void getRptIncome(IncomeTotalReport incomeReport, List<RelTbRptIncome> incomeList,
                              Long saveId) throws IllegalAccessException, NoSuchMethodException,
            InvocationTargetException {
        List<IncomeReportDTO> incomeReportDTOS = incomeReport.getIncomeList();
        if (CollectionUtils.isNotEmpty(incomeReportDTOS)) {
            for (IncomeReportDTO incomeReportDTO : incomeReportDTOS) {
                if (null == incomeReportDTO) {
                    continue;
                }
                RelTbRptIncome relTbRptIncome = new RelTbRptIncome();
                relTbRptIncome.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbRptIncome, incomeReportDTO);
                incomeList.add(relTbRptIncome);
            }
        }
    }

    private void getRptRepayment(ExpenditureReport expenditureReport,
                                 List<RelTbRptRepayment> repaymentList, Long saveId) throws IllegalAccessException,
            NoSuchMethodException, InvocationTargetException {
        List<RepaymentReportDTO> repaymentReportDTOS = expenditureReport.getRepaymentList();
        if (CollectionUtils.isNotEmpty(repaymentReportDTOS)) {
            for (RepaymentReportDTO repaymentReportDTO : repaymentReportDTOS) {
                if (null == repaymentReportDTO) {
                    continue;
                }
                RelTbRptRepayment relTbRptRepayment = new RelTbRptRepayment();
                relTbRptRepayment.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbRptRepayment, repaymentReportDTO);
                repaymentList.add(relTbRptRepayment);
            }
        }
    }

    private void getRptConsumption(ExpenditureReport expenditureReport,
                                   List<RelTbRptConsumption> consumptionList, Long saveId) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        List<ConsumptionReportDTO> consumptionReportDTOS = expenditureReport.getConsumptionList();
        if (CollectionUtils.isNotEmpty(consumptionReportDTOS)) {
            for (ConsumptionReportDTO consumptionReportDTO : consumptionReportDTOS) {
                if (null == consumptionReportDTO) {
                    continue;
                }
                RelTbRptConsumption relTbRptConsumption = new RelTbRptConsumption();
                relTbRptConsumption.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbRptConsumption, consumptionReportDTO);
                consumptionList.add(relTbRptConsumption);
            }
        }
    }

    private void getRptShop(ReportSummary reportSummary, List<RelTbRptShop> shopList,
                            Long saveId) throws IllegalAccessException, NoSuchMethodException,
            InvocationTargetException {
        List<TaobaoShopReportDTO> shopReportDTOS = reportSummary.getTaobaoShopList();
        if (CollectionUtils.isNotEmpty(shopReportDTOS)) {
            for (TaobaoShopReportDTO shopReportDTO : shopReportDTOS) {
                if (null == shopReportDTO) {
                    continue;
                }
                RelTbRptShop relTbRptShop = new RelTbRptShop();
                relTbRptShop.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbRptShop, shopReportDTO);
                shopList.add(relTbRptShop);
            }
        }
    }

    private void getRptAddress(ReportSummary reportSummary, List<RelTbRptAddress> addressList,
                               Long saveId) throws IllegalAccessException, NoSuchMethodException,
            InvocationTargetException {
        List<TaobaoAddressReportDTO> addressReportDTOS = reportSummary.getTaobaoAddressList();
        if (CollectionUtils.isNotEmpty(addressReportDTOS)) {
            for (TaobaoAddressReportDTO addressReportDTO : addressReportDTOS) {
                if (null == addressReportDTO) {
                    continue;
                }
                RelTbRptAddress relTbRptAddress = new RelTbRptAddress();
                relTbRptAddress.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbRptAddress, addressReportDTO);
                addressList.add(relTbRptAddress);
            }
        }
    }

    private void getIntimacyPay(BaseInfoReportDTO baseInfo,
                                List<RelTbRptIntimacyPay> intimacyPayList, Long saveId) throws IllegalAccessException
            , NoSuchMethodException, InvocationTargetException {
        List<IntimacyPayDTO> intimacyPayDTOS = baseInfo.getIntimacyPayList();
        if (CollectionUtils.isNotEmpty(intimacyPayDTOS)) {
            for (IntimacyPayDTO intimacyPayDTO : intimacyPayDTOS) {
                if (null == intimacyPayDTO) {
                    continue;
                }
                RelTbRptIntimacyPay relTbRptIntimacyPay = new RelTbRptIntimacyPay();
                relTbRptIntimacyPay.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbRptIntimacyPay, intimacyPayDTO);
                intimacyPayList.add(relTbRptIntimacyPay);
            }
        }
    }

    private void getBankRepayPlan(UserEcommerceDataDTO jsonRootBean,
                                  List<RelTbBankRepayPlan> tbBankRepayPlanList, Long saveId) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        List<MyBankRepayPlanDTO> myBankRepayPlanList = jsonRootBean.getMyBankRepayPlanList();
        if (CollectionUtils.isNotEmpty(myBankRepayPlanList)) {
            for (MyBankRepayPlanDTO myBankRepayPlanDTO : myBankRepayPlanList) {
                if (null == myBankRepayPlanDTO) {
                    continue;
                }
                RelTbBankRepayPlan relTbBankRepayPlan = new RelTbBankRepayPlan();
                relTbBankRepayPlan.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbBankRepayPlan, myBankRepayPlanDTO);
                tbBankRepayPlanList.add(relTbBankRepayPlan);
            }
        }
    }

    private void getBankLoanDetail(UserEcommerceDataDTO jsonRootBean,
                                   List<RelTbBankLoanDetail> tbBankLoanDetailList, Long saveId) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        List<MyBankLoanDetailDTO> myBankLoanDetails = jsonRootBean.getMyBankLoanDetails();
        if (CollectionUtils.isNotEmpty(myBankLoanDetails)) {
            for (MyBankLoanDetailDTO myBankLoanDetailDTO : myBankLoanDetails) {
                if (null == myBankLoanDetailDTO) {
                    continue;
                }
                RelTbBankLoanDetail relTbBankLoanDetail = new RelTbBankLoanDetail();
                relTbBankLoanDetail.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbBankLoanDetail, myBankLoanDetailDTO);
                tbBankLoanDetailList.add(relTbBankLoanDetail);
            }
        }
    }

    private void getSubOrder(UserEcommerceDataDTO jsonRootBean, List<RelTbSubOrder> subOrderList,
                             Long saveId) throws IllegalAccessException, NoSuchMethodException,
            InvocationTargetException {
        List<TaobaoOrderDTO> Orders = jsonRootBean.getTaobaoOrders();

        if (CollectionUtils.isNotEmpty(Orders)) {
            for (TaobaoOrderDTO order : Orders) {
                if (null == order) {
                    continue;
                }
                List<TaobaoSubOrderDTO> subOrders = order.getSubOrders();
                if (CollectionUtils.isNotEmpty(subOrders)) {
                    for (TaobaoSubOrderDTO subOrder : subOrders) {
                        if (null == subOrder) {
                            continue;
                        }
                        TaobaoSellerDTO seller = order.getSeller();
                        TaobaoLogisticsDTO logistics = order.getLogistics();
                        EcommerceAddressDTO address = order.getAddress();
                        RelTbSubOrder relTbSubOrder = new RelTbSubOrder();
                        relTbSubOrder.setBaseId(saveId);

                        PropertyUtils.copyProperties(relTbSubOrder, subOrder);
                        if (null != logistics) {
                            PropertyUtils.copyProperties(relTbSubOrder, logistics);
                            relTbSubOrder.setCreateTimeLogistics(logistics.getCreateTime());
                        }
                        if (null != seller) {
                            PropertyUtils.copyProperties(relTbSubOrder, seller);
                        }
                        if (null != address) {
                            PropertyUtils.copyProperties(relTbSubOrder, address);
                            relTbSubOrder.setAddressSub(address.getAddress());
                        }
                        PropertyUtils.copyProperties(relTbSubOrder, order);
                        subOrderList.add(relTbSubOrder);
                    }
                } else {
                    TaobaoSellerDTO seller = order.getSeller();
                    TaobaoLogisticsDTO logistics = order.getLogistics();
                    EcommerceAddressDTO address = order.getAddress();
                    RelTbSubOrder relTbSubOrder = new RelTbSubOrder();
                    relTbSubOrder.setBaseId(saveId);
                    if (null != logistics) {
                        PropertyUtils.copyProperties(relTbSubOrder, logistics);
                        relTbSubOrder.setCreateTimeLogistics(logistics.getCreateTime());
                    }
                    if (null != seller) {
                        PropertyUtils.copyProperties(relTbSubOrder, seller);
                    }
                    if (null != address) {
                        PropertyUtils.copyProperties(relTbSubOrder, address);
                        relTbSubOrder.setAddressSub(address.getAddress());
                    }
                    PropertyUtils.copyProperties(relTbSubOrder, order);
                    subOrderList.add(relTbSubOrder);
                }
            }
        }
    }

    private void getSoldSubOrder(UserEcommerceDataDTO jsonRootBean,
                                 List<RelTbSoldSubOrder> soldSubOrderList, Long saveId) throws IllegalAccessException
            , NoSuchMethodException, InvocationTargetException {
        List<TaobaoBOrderDTO> soldOrders = jsonRootBean.getSoldOrders();

        if (CollectionUtils.isNotEmpty(soldOrders)) {
            for (TaobaoBOrderDTO soldOrder : soldOrders) {
                if (null == soldOrder) {
                    continue;
                }
                List<TaobaoBSubOrderDTO> subOrders = soldOrder.getSubOrders();
                if (CollectionUtils.isNotEmpty(subOrders)) {
                    for (TaobaoBSubOrderDTO subOrder : subOrders) {
                        if (null == subOrder) {
                            continue;
                        }
                        TaobaoBuyerDTO buyer = soldOrder.getBuyer();
                        TaobaoBLogisticsDTO logistics = soldOrder.getLogistics();
                        RelTbSoldSubOrder relTbSoldSubOrder = new RelTbSoldSubOrder();
                        relTbSoldSubOrder.setBaseId(saveId);
                        PropertyUtils.copyProperties(relTbSoldSubOrder, subOrder);
                        relTbSoldSubOrder.setOrderNumberSub(subOrder.getOrderNumber());
                        if (null != logistics) {
                            PropertyUtils.copyProperties(relTbSoldSubOrder, logistics);
                            relTbSoldSubOrder.setCreateTimeLogistics(logistics.getCreateTime());
                        }
                        if (null != buyer) {
                            PropertyUtils.copyProperties(relTbSoldSubOrder, buyer);
                        }
                        PropertyUtils.copyProperties(relTbSoldSubOrder, soldOrder);
                        soldSubOrderList.add(relTbSoldSubOrder);
                    }
                } else {
                    TaobaoBuyerDTO buyer = soldOrder.getBuyer();
                    TaobaoBLogisticsDTO logistics = soldOrder.getLogistics();
                    RelTbSoldSubOrder relTbSoldSubOrder = new RelTbSoldSubOrder();
                    relTbSoldSubOrder.setBaseId(saveId);
                    if (null != logistics) {
                        PropertyUtils.copyProperties(relTbSoldSubOrder, logistics);
                        relTbSoldSubOrder.setCreateTimeLogistics(logistics.getCreateTime());
                    }
                    if (null != buyer) {
                        PropertyUtils.copyProperties(relTbSoldSubOrder, buyer);
                    }
                    PropertyUtils.copyProperties(relTbSoldSubOrder, soldOrder);
                    soldSubOrderList.add(relTbSoldSubOrder);
                }
            }
        }
    }

    private void getTransferBankCard(UserEcommerceDataDTO jsonRootBean,
                                     List<RelTbTransferBankCard> tbTransferBankCardList,
                                     Long saveId) throws IllegalAccessException,
            NoSuchMethodException, InvocationTargetException {
        List<EcommerceTransferBankCardDTO> transferBankCardDTOS =
                jsonRootBean.getTransferBankCards();

        if (CollectionUtils.isNotEmpty(transferBankCardDTOS)) {
            for (EcommerceTransferBankCardDTO transferBankCardDTO : transferBankCardDTOS) {
                if (null == transferBankCardDTO) {
                    continue;
                }
                RelTbTransferBankCard relTbTransferBankCard = new RelTbTransferBankCard();
                relTbTransferBankCard.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbTransferBankCard, transferBankCardDTO);
                tbTransferBankCardList.add(relTbTransferBankCard);
            }
        }
    }

    private void getBankAssetDetail(UserEcommerceDataDTO jsonRootBean,
                                    List<RelTbBankAssetDetail> tbBankAssetDetailList,
                                    Long saveId) throws IllegalAccessException,
            NoSuchMethodException, InvocationTargetException {
        List<MyBankAssetDetailDTO> myBankAssetDetails = jsonRootBean.getMyBankAssetDetails();

        if (CollectionUtils.isNotEmpty(myBankAssetDetails)) {
            for (MyBankAssetDetailDTO myBankAssetDetailDTO : myBankAssetDetails) {
                if (null == myBankAssetDetailDTO) {
                    continue;
                }
                RelTbBankAssetDetail relTbBankAssetDetail = new RelTbBankAssetDetail();
                relTbBankAssetDetail.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbBankAssetDetail, myBankAssetDetailDTO);
                tbBankAssetDetailList.add(relTbBankAssetDetail);
            }
        }
    }

    private void getTrade(UserEcommerceDataDTO jsonRootBean, List<RelTbTrade> tradeList,
                          Long saveId) throws IllegalAccessException, NoSuchMethodException,
            InvocationTargetException {
        List<EcommerceTradeDTO> tradeDTOS = jsonRootBean.getEcommerceTrades();

        if (CollectionUtils.isNotEmpty(tradeDTOS)) {
            for (EcommerceTradeDTO tradeDTO : tradeDTOS) {
                if (null == tradeDTO) {
                    continue;
                }
                RelTbTrade relTbTrade = new RelTbTrade();
                relTbTrade.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbTrade, tradeDTO);
                tradeList.add(relTbTrade);
            }
        }
    }

    private void getHuabeiConsume(UserEcommerceDataDTO jsonRootBean,
                                  List<RelTbHuabeiConsume> huabeiConsumeList, Long saveId) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        List<HuabeiConsumeDTO> huabeiConsumeDTOS = jsonRootBean.getHuabeiConsumeList();

        if (CollectionUtils.isNotEmpty(huabeiConsumeDTOS)) {
            for (HuabeiConsumeDTO huabeiConsumeDTO : huabeiConsumeDTOS) {
                if (null == huabeiConsumeDTO) {
                    continue;
                }
                RelTbHuabeiConsume relTbHuabeiConsume = new RelTbHuabeiConsume();
                relTbHuabeiConsume.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbHuabeiConsume, huabeiConsumeDTO);
                huabeiConsumeList.add(relTbHuabeiConsume);
            }
        }
    }

    private void getHuabeiBill(UserEcommerceDataDTO jsonRootBean,
                               List<RelTbHuabeiBill> huabeiBillList, Long saveId) throws IllegalAccessException,
            NoSuchMethodException, InvocationTargetException {
        List<TaobaoHuabeiBillDTO> huabeiBillDTOS = jsonRootBean.getHuabeiBills();

        if (CollectionUtils.isNotEmpty(huabeiBillDTOS)) {
            for (TaobaoHuabeiBillDTO huabeiBillDTO : huabeiBillDTOS) {
                if (null == huabeiBillDTO) {
                    continue;
                }
                RelTbHuabeiBill relTbHuabeiBill = new RelTbHuabeiBill();
                relTbHuabeiBill.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbHuabeiBill, huabeiBillDTO);
                huabeiBillList.add(relTbHuabeiBill);
            }
        }
    }

    private void getFeesAccount(UserEcommerceDataDTO jsonRootBean,
                                List<RelTbFeesAccount> feesAccountList, Long saveId) throws IllegalAccessException,
            NoSuchMethodException, InvocationTargetException {
        List<EcommerceFeesAccountDTO> feesAccountDTOS = jsonRootBean.getEcommercePaymentAccounts();

        if (CollectionUtils.isNotEmpty(feesAccountDTOS)) {
            for (EcommerceFeesAccountDTO feesAccountDTO : feesAccountDTOS) {
                if (null == feesAccountDTO) {
                    continue;
                }
                RelTbFeesAccount relTbFeesAccount = new RelTbFeesAccount();
                relTbFeesAccount.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbFeesAccount, feesAccountDTO);
                feesAccountList.add(relTbFeesAccount);
            }
        }
    }

    private void getBindedBankCard(UserEcommerceDataDTO jsonRootBean,
                                   List<RelTbBindedBankCard> bindedBankCardList, Long saveId) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        List<EcommerceBindedBankCardDTO> bindedBankCardDTOS =
                jsonRootBean.getEcommerceBindedBankCards();

        if (CollectionUtils.isNotEmpty(bindedBankCardDTOS)) {
            for (EcommerceBindedBankCardDTO bindedBankCardDTO : bindedBankCardDTOS) {
                if (null == bindedBankCardDTO) {
                    continue;
                }
                RelTbBindedBankCard relTbBindedBankCard = new RelTbBindedBankCard();
                relTbBindedBankCard.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbBindedBankCard, bindedBankCardDTO);
                bindedBankCardList.add(relTbBindedBankCard);
            }
        }
    }

    private void getAddress(UserEcommerceDataDTO jsonRootBean, List<RelTbAddress> addressList,
                            Long saveId) throws IllegalAccessException, NoSuchMethodException,
            InvocationTargetException {
        List<EcommerceAddressDTO> addressDTOList = jsonRootBean.getEcommerceConsigneeAddresses();

        if (CollectionUtils.isNotEmpty(addressDTOList)) {
            for (EcommerceAddressDTO addressDTO : addressDTOList) {
                if (null == addressDTO) {
                    continue;
                }
                RelTbAddress relTbAddress = new RelTbAddress();
                relTbAddress.setBaseId(saveId);
                PropertyUtils.copyProperties(relTbAddress, addressDTO);
                addressList.add(relTbAddress);
            }
        }
    }


    @Override
    public void save(TbRelativizeResult tbRelativizeResult) {
        if (null == tbRelativizeResult) {
            return;
        }
        RelTbUserData relTbUserData = tbRelativizeResult.getRelTbUserData();
        if (null != relTbUserData) {
            Map<String, Object> map = new HashMap<>();
            map.put("phone", relTbUserData.getMobile());
            map.put("idCard", relTbUserData.getIdentityCard());
            int num = relTbUserDataDao.countByIdCardAndPhone(map);
            if (num > 0) {
                return;
            }
            relTbUserDataDao.insert(relTbUserData);
        }
        List<RelTbHuabeiBill> relTbHuabeiBillList = tbRelativizeResult.getRelTbHuabeiBillList();
        if (CollectionUtils.isNotEmpty(relTbHuabeiBillList)) {
            relTbHuabeiBillDao.insertList(relTbHuabeiBillList);
        }
        List<RelTbAddress> relTbAddressList = tbRelativizeResult.getRelTbAddressList();
        if (CollectionUtils.isNotEmpty(relTbAddressList)) {
            relTbAddressDao.insertList(relTbAddressList);
        }
        List<RelTbTrade> relTbTradeList = tbRelativizeResult.getRelTbTradeList();
        if (CollectionUtils.isNotEmpty(relTbTradeList)) {
            relTbTradeDao.insertList(relTbTradeList);
        }
        List<RelTbFeesAccount> relTbFeesAccountList = tbRelativizeResult.getRelTbFeesAccountList();
        if (CollectionUtils.isNotEmpty(relTbFeesAccountList)) {
            relTbFeesAccountDao.insertList(relTbFeesAccountList);
        }


        List<RelTbSubOrder> relTbSubOrderList = tbRelativizeResult.getRelTbSubOrderList();
        if (CollectionUtils.isNotEmpty(relTbSubOrderList)) {
            relTbSubOrderDao.insertList(relTbSubOrderList);
        }
        List<RelTbBindedBankCard> relTbBindedBankCardList =
                tbRelativizeResult.getRelTbBindedBankCardList();
        if (CollectionUtils.isNotEmpty(relTbBindedBankCardList)) {
            relTbBindedBankCardDao.insertList(relTbBindedBankCardList);
        }
        List<RelTbSoldSubOrder> relTbSoldSubOrderList =
                tbRelativizeResult.getRelTbSoldSubOrderList();
        if (CollectionUtils.isNotEmpty(relTbSoldSubOrderList)) {
            relTbSoldSubOrderDao.insertList(relTbSoldSubOrderList);
        }
        List<RelTbTransferBankCard> relTbTransferBankCardList =
                tbRelativizeResult.getRelTbTransferBankCardList();
        if (CollectionUtils.isNotEmpty(relTbTransferBankCardList)) {
            relTbTransferBankCardDao.insertList(relTbTransferBankCardList);
        }
        List<RelTbHuabeiConsume> relTbHuabeiConsumeList =
                tbRelativizeResult.getRelTbHuabeiConsumeList();
        if (CollectionUtils.isNotEmpty(relTbHuabeiConsumeList)) {
            relTbHuabeiConsumeDao.insertList(relTbHuabeiConsumeList);
        }
        List<RelTbBankAssetDetail> relTbBankAssetDetailList =
                tbRelativizeResult.getRelTbBankAssetDetailList();
        if (CollectionUtils.isNotEmpty(relTbBankAssetDetailList)) {
            relTbBankAssetDetailDao.insertList(relTbBankAssetDetailList);
        }
        List<RelTbBankLoanDetail> relTbBankLoanDetailList =
                tbRelativizeResult.getRelTbBankLoanDetailList();
        if (CollectionUtils.isNotEmpty(relTbBankLoanDetailList)) {
            relTbBankLoanDetailDao.insertList(relTbBankLoanDetailList);
        }
        List<RelTbBankRepayPlan> relTbBankRepayPlanList =
                tbRelativizeResult.getRelTbBankRepayPlanList();
        if (CollectionUtils.isNotEmpty(relTbBankRepayPlanList)) {
            relTbBankRepayPlanDao.insertList(relTbBankRepayPlanList);
        }
    }

    @Override
    public void saveReportResult(TbReportRelativizeResult tbReportRelativizeResult) {
        if (null == tbReportRelativizeResult) {
            return;
        }

        RelTbRptBaseInfo relTbRptBaseInfo = tbReportRelativizeResult.getRelTbRptBaseInfo();
        if (relTbRptBaseInfo != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("phone", relTbRptBaseInfo.getPhone());
            map.put("idCard", relTbRptBaseInfo.getIdCard());
            int num = relTbRptBaseInfoDao.countByIdCardAndPhone(map);
            if (num > 0) {
                return;
            }
            relTbRptBaseInfoDao.insert(relTbRptBaseInfo);
        }
        List<RelTbRptAddress> relTbRptAddressList =
                tbReportRelativizeResult.getRelTbRptAddressList();
        if (CollectionUtils.isNotEmpty(relTbRptAddressList)) {
            relTbRptAddressDao.insertList(relTbRptAddressList);
        }
        List<RelTbRptShop> relTbRptShopList =
                tbReportRelativizeResult.getRelTbRptShopList();
        if (CollectionUtils.isNotEmpty(relTbRptShopList)) {
            relTbRptShopDao.insertList(relTbRptShopList);
        }
        List<RelTbRptConsumption> relTbRptConsumptionList =
                tbReportRelativizeResult.getRelTbRptConsumptionList();
        if (CollectionUtils.isNotEmpty(relTbRptConsumptionList)) {
            relTbRptConsumptionDao.insertList(relTbRptConsumptionList);
        }
        List<RelTbRptRepayment> relTbRptRepaymentList =
                tbReportRelativizeResult.getRelTbRptRepaymentList();
        if (CollectionUtils.isNotEmpty(relTbRptRepaymentList)) {
            relTbRptRepaymentDao.insertList(relTbRptRepaymentList);
        }
        List<RelTbRptIntimacyPay> relTbRptIntimacyPayList =
                tbReportRelativizeResult.getRelTbRptIntimacyPayList();
        if (CollectionUtils.isNotEmpty(relTbRptIntimacyPayList)) {
            relTbRptIntimacyPayDao.insertList(relTbRptIntimacyPayList);
        }
        List<RelTbRptIncome> relTbRptIncomeList =
                tbReportRelativizeResult.getRelTbRptIncomeList();
        if (CollectionUtils.isNotEmpty(relTbRptIncomeList)) {
            relTbRptIncomeDao.insertList(relTbRptIncomeList);
        }
    }

    @Override
    public void saveZhima(RelData relData) {
        if (null == relData) {
            return;
        }

        List<RelTbZhima> relTbZhimaList = relData.getZmCredits();
        if (CollectionUtils.isNotEmpty(relTbZhimaList)) {
            relTbZhimaMapper.insertList(relTbZhimaList);
        }
    }

}
